﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using HashMaster.Lib;

namespace HashMaster.Service
{
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.	
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class JobService
    {



        /// <summary>
        /// 提交进度并获取任务
        /// </summary>
        /// <param name="clientName"></param>
        /// <param name="macAddress"></param>
        /// <returns></returns>
        [WebInvoke(UriTemplate = "", Method = "POST")]
        public ClientSyncData ClientSync(ClientSyncData clientData)
        {
            ClientSyncData ret = new ClientSyncData();

            ///如果jobid！=0 更新任务进度
            if (clientData.JobId != 0)
            {
                if (Utility.SetJobProgress(clientData)==false)
                {
                    ///更新任务进度失败
                    ret.ErrorCode = 202;
                }
            }

            ///激活成功 返回客户端当前任务信息 如果未被分配任务则分配一个未分配状态的任务
            ret = Utility.GetJobByClient(clientData);
            ret.ClientName = clientData.ClientName;
            ret.MacAddress = clientData.MacAddress;
            if (ret.JobId==clientData.JobId)
            {
                ret.Progress = clientData.Progress;
            }
            return ret;
        }


        /*

        /// <summary>
        ///  客户机请求新任务
        /// </summary>
        /// <param name="clientName"></param>
        /// <param name="macAddress"></param>
        /// <returns></returns>
        [WebInvoke(UriTemplate = "{clientName}/{macAddress}", Method = "PUT")]
        public JobParameter RequestJob(string clientName, string macAddress)
        {
            return Utility.RequestJob(clientName, macAddress);
        }
         */

        /// <summary>
        /// 收回任务
        /// </summary>
        /// <param name="clientName"></param>
        /// <param name="macAddress"></param>
        /// <param name="taskHash"></param>
        /// <returns></returns>
        //[WebInvoke(UriTemplate = "{clientName}/{macAddress}/{taskHash}/{result}", Method = "PUT")]
        //public bool SubmitJob(string clientName, string macAddress, string taskHash,string result)
        //{
        //    return Utility.SubmitJob(clientName,macAddress,taskHash,result);
        //}

        /*
        [WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
        public void Delete(string id)
        {
            // TODO: Remove the instance of SampleItem with the given id from the collection
            throw new NotImplementedException();
        }
        */
    }
}
